package com.jhws.common.pay.service;

import com.jhws.business.customer.bo.PayCustomerSecret;
import com.jhws.business.customer.service.PayCustService;
import com.jhws.common.pay.bo.IOrderPayModule;
import com.jhws.common.pay.bo.Order;
import com.jhws.common.pay.bo.PayConstants;
import com.jhws.common.pay.bo.PayModuleInfo;
import com.jhws.core.db.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单服务
 *
 * 处理订单的接口，以及提供订单回调注册与处理回调
 */
@Service
public class OrderService {

    @Resource
    BaseDao BaseDao;
    @Resource
    PayCustService IPayCustService;

    Map<String,IOrderPayModule> modules = new HashMap<>();

    public void moduleLegalAssert(String module) throws Exception{
        if(modules.get(module.toUpperCase()) == null)
            throw new Exception("无法识别的模块"+module);
    }

    /* 注册订单处理模块 */
    public void registerModule(IOrderPayModule module){
        for(String name : module.getModuleName()){
            modules.put(name.toUpperCase(),module);
        }
    }

    /* 订单完成时调用相应模块回调 */
    @Transactional
    public void orderPayCallback(Order order) throws Exception {
        modules.get(order.getOrderType().toUpperCase()).handle(order);
    }

    public boolean orderPreCheck(Order order){
        return modules.get(order.getOrderType().toUpperCase()).preCheck(order);
    }

    public Order save(Order order){
        BaseDao.saveOrUpdate(order);
        return order;
    }

    public PayCustomerSecret findCustSecretByOrder(Order order) throws Exception{
        return modules.get(order.getOrderType().toUpperCase()).findCustomerSecret(order);
    }

    public Order findOrderById(Long id){
        return BaseDao.get(Order.class,id);
    }

    public Map findOrdersPage(String commCode,String dateStart,String dateEnd, String payModuleName){
        String where = "1=1";
        if(StringUtils.isEmpty(commCode))
            return new HashMap();
        where += " and commCode = '"+commCode+"'";

        if(StringUtils.isNotEmpty(dateStart) && StringUtils.isNotEmpty(dateEnd))
            where += " and time > '"+dateStart+"' and time < '"+dateEnd+"'";

        if(StringUtils.isNotEmpty(payModuleName))
            where += " and pay_module = '"+payModuleName+"'";
        return BaseDao.queryPage("from Order where "+where +" order by time desc");
    }

    public List<Order> findOrders(String commCode,String dateStart,String dateEnd, String payModuleName, Integer state){
        String where = "1=1";
        if(StringUtils.isNotEmpty(commCode))
            where += " and commCode = '"+commCode+"'";

        if(StringUtils.isNotEmpty(dateStart) && StringUtils.isNotEmpty(dateEnd))
            where += " and time > '"+dateStart+"' and time < '"+dateEnd+"'";

        if(StringUtils.isNotEmpty(payModuleName))
            where += " and pay_module = '"+payModuleName+"'";

        if(state != null)
            where += " and order_state = '"+state+"'";

        return BaseDao.findList("from Order where "+where);
    }

    /**
     * 获取订单详情
     * @param orderId
     * @return
     */
    public Map getDetailInfoForOrder(Long orderId){
        Order order = findOrderById(orderId);
        return modules.get(order.getOrderType().toUpperCase()).getDetailInfoForOrder(order);
    }

    /**
     * 获取对应支付类型的中文描述
     * @param orderType
     * @return
     */
    public String getModuleDesc(String orderType){
        String [] name = modules.get(orderType).getModuleName();
        String [] desc = modules.get(orderType).getModuleDesc();
        if(desc.length != name.length)
            return orderType;

        for(int i=0; i < name.length; i++){
            if(name[i].equals(orderType))
                return desc[i];
        }

        return orderType;
    }

    /**
     * 获取支付模块类别名称
     * @param orderType
     * @return
     */
    public String getPayModuleName(String orderType){
        return modules.get(orderType).getPayModuleName();
    }

    /**
     * 获取对应模块的支付实体信息
     * @param orderType 订单类型，这里传每个模块的基本类型(假如一个模块有很多个订单类型，可能要新建一个基本的)
     * @param commCode
     * @return
     */
    public List<PayModuleInfo> getPayModulesInfo(String orderType,String commCode){
        return modules.get(orderType).getPayModuleInfo(orderType,commCode);
    }

    /**
     * 设置某个支付类型实体的绑定支付ID，比如给某个小区设置绑定，某个停车场设置绑定等
     * @param orderType  支付订单类型，也就是支付类型
     * @param id  支付实体ID，可能是小区编号、停车场编号等
     * @param customId  支付客户ID，也就是Web用户自定义的支付组ID
     * @throws Exception
     */
    public void setModuleCustomId(String orderType, String id, Long customId) throws Exception{
        modules.get(orderType).setCustomerId(id,customId);
    }
}
